$(function () {
    $("#schedule_datagrid").datagrid({
        fit: true,
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        pageSize: 10,
        title: "选课情况",
        url: 'schedule/list/current',
        view: detailview,
        detailFormatter: function (index, row) {
            return '<div style="padding:2px"><table id="ddv-' + index + '"></table></div>';
        },
        onExpandRow: function (index, row) {
            $('#ddv-'+index).datagrid({
                url:'elective/list/situation/'+row.id,
                fitColumns:true,
                singleSelect:true,
                rownumbers:true,
                height:'auto',
                columns:[[
                    {field: 'electiveWeek', width: 150, align: 'center', title: '课程周期',
                        formatter: function (value, row, index) {
                            return {
                                1: "周一",
                                2: "周二",
                                4: "周三",
                                8: "周四",
                                16: "周五",
                                32: "周六",
                                64: "周日"
                            }[value] || undefined;
                        }
                    },
                    {field: 'electiveTime', width: 150, align: 'center', title: '课程节次',
                        formatter: function (value, row, index) {
                            return  {
                                1: "第一节课",
                                2: "第二节课",
                                4: "第三节课",
                                8: "第四节课",
                                16: "第五节课",
                                32: "第六节课",
                                64: "第七节课",
                                128: "第八节课"
                            }[value] || undefined;
                        }
                    },
                    {field: 'currentNum', width: 150, align: 'center', title: '当前选课人数',
                        styler:function(value,row,index){
                            return 'color:red;';
                        }
                    }
                ]],
                onResize:function(){
                    $('#schedule_datagrid').datagrid('fixDetailRowHeight',index);
                },
                onLoadSuccess:function(){
                    setTimeout(function(){
                        $('#schedule_datagrid').datagrid('fixDetailRowHeight',index);
                    },0);
                },
                onClickRow: function (rowIndex, rowData) {
                    $.ajax({
                        type: 'GET',
                        url: "credit/" + rowData.schedulingId + "/student-course-credit",
                        dataType: "json",
                        cache: false,
                        success: function (data) {
                            if (data) {
                            	$('#grade').text(new Date().getFullYear());
                            	$('#courseName').text(row.courseName);
                            	$('#teacher').text(row.teacherName);
                            	$.each(data, function(i, item) {
                            		var tr = "<tr>";
                    				tr += "<td>" + ++i + "</td>";
                    				tr += "<td>" + item.studentNumber + "</td>";
                    				tr += "<td colspan='3'>" + item.studentName + "</td>";
                    				if (item.isGetCredit == 1) {
    									tr += "<td colspan='3'>是</td>";
    								} else {
    									tr += "<td colspan='3'>否</td>";
    								}
    								tr += "</tr>";
    								content += tr;
    							});
    							$('#data').append(content);
                            }
                        }
                    });
                }
            });
            $('#schedule_datagrid').datagrid('fixDetailRowHeight',index);
        },
        columns: [
            [
                {field: 'courseId', width: 200, align: 'center', title: '课程名称',
                    formatter: function (value, row, index) {
                        if (value == 0) {
                            return null;
                        }
                        return row.courseName;
                    }
                },
                {field: 'teacherId', width: 150, align: 'center', title: '授课老师',
                    formatter: function (value, row, index) {
                        if (value == 0) {
                            return null;
                        }
                        return row.teacherName;
                    }
                },
                {field: 'classroomId', width: 180, align: 'center', title: '授课地点',
                    formatter: function (value, row, index) {
                        if (value == 0) {
                            return null;
                        }
                        return row.classroomLocation;
                    }
                },
                {field: 'restrictionNumber', width: 80, align: 'center', title: '开班人数'}
            ]
        ],
        loadFilter:pageLoadFilter
    });
});


function print() {
    $('#creditArea').jqprint();
}